package com.pajx.facelibrary.utils;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class FileUitls {

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    private static String dateFormat;

    public FileUitls() {
    }

    public static boolean isSdCardAvailable() {
        return Environment.getExternalStorageState().equals("mounted");
    }

    public static File getSDRootFile() {
        return isSdCardAvailable() ? Environment.getExternalStorageDirectory() : null;
    }

    public static File getFaceDirectory() {
        File sdRootFile = getSDRootFile();
        File file = null;
        if (sdRootFile != null && sdRootFile.exists()) {
            file = new File(sdRootFile, "faces");
            if (!file.exists()) {
                boolean var2 = file.mkdirs();
            }
        }

        return file;
    }

    public static File getBatchFaceDirectory(String batchDir) {
        File sdRootFile = getSDRootFile();
        File file = null;
        if (sdRootFile != null && sdRootFile.exists()) {
            file = new File(sdRootFile, batchDir);
            if (!file.exists()) {
                boolean var3 = file.mkdirs();
            }
        }

        return file;
    }

    public static boolean saveFile(File file, Bitmap bitmap) {
        FileOutputStream out = null;

        try {
            out = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
            boolean var3 = true;
            return var3;
        } catch (Exception var13) {
            var13.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (Exception var12) {
                var12.printStackTrace();
            }

        }

        return false;
    }

    public static void deleteLicense(Context context, String licenseName) {
        String filePath = context.getFilesDir().getParent() + "/" + licenseName;
        File file = new File(filePath);
        if (file.exists()) {
            file.delete();
        }

        File var4 = context.getDir(licenseName, 0);
        if (var4 != null && var4.exists()) {
            var4.delete();
        }

    }

    public static boolean fileIsExists(String strFile) {
        try {
            File f = new File(strFile);
            if (!f.exists()) {
                Log.i("wtf", "file_state->file not exits");
                return false;
            } else {
                return true;
            }
        } catch (Exception var2) {
            Log.i("wtf", "file_state->" + var2.getMessage());
            return false;
        }
    }

    public static String readFile(String strFilePath) {
        String content = "";
        File file = new File(strFilePath);
        if (file.isDirectory()) {
            Log.d("TestFile", "The File doesn't not exist.");
        } else {
            try {
                InputStream instream = new FileInputStream(file);
                if (instream != null) {
                    InputStreamReader inputreader = new InputStreamReader(instream);

                    String line;
                    for(BufferedReader buffreader = new BufferedReader(inputreader); (line = buffreader.readLine()) != null; content = line) {
                    }

                    instream.close();
                }
            } catch (FileNotFoundException var8) {
                Log.d("TestFile", "The File doesn't not exist.");
            } catch (IOException var9) {
                Log.d("TestFile", var9.getMessage());
            }
        }

        return content;
    }

    public static ArrayList<String> readLicense(String strFilePath) {
        ArrayList<String> licenseList = new ArrayList();
        String content = "";
        File file = new File(strFilePath);
        if (file.isDirectory()) {
            Log.d("TestFile", "The File doesn't not exist.");
        } else {
            try {
                InputStream instream = new FileInputStream(file);
                if (instream != null) {
                    InputStreamReader inputreader = new InputStreamReader(instream);
                    BufferedReader buffreader = new BufferedReader(inputreader);

                    String line;
                    while((line = buffreader.readLine()) != null) {
                        licenseList.add(line);
                    }

                    instream.close();
                }
            } catch (FileNotFoundException var9) {
                Log.d("TestFile", "The File doesn't not exist.");
            } catch (IOException var10) {
                Log.d("TestFile", var10.getMessage());
            }
        }

        return licenseList;
    }

    public static String getSDPath() {
        File sdDir = null;
        boolean sdCardExist = Environment.getExternalStorageState().equals("mounted");
        if (sdCardExist) {
            sdDir = Environment.getExternalStorageDirectory();
        }

        return sdDir != null ? sdDir.toString() : null;
    }

    public static boolean assetOpen(Context context, String modelPath) {
        boolean exist = false;
        if (context != null && !TextUtils.isEmpty(modelPath)) {
            try {
                context.getAssets().open(modelPath);
                exist = true;
            } catch (Exception var4) {
            }

            return exist;
        } else {
            return false;
        }
    }

    public static byte[] getModelContent(Context context, String modelName) {
        InputStream is = null;
        byte[] bytes = new byte[0];
        File file = new File(modelName);
        if (file.exists()) {
            try {
                is = new FileInputStream(file);
                bytes = new byte[((InputStream)is).available()];
                ((InputStream)is).read(bytes);
            } catch (FileNotFoundException var36) {
            } catch (IOException var37) {
            } finally {
                try {
                    if (is != null) {
                        ((InputStream)is).close();
                    }
                } catch (IOException var33) {
                }

            }
        }

        if (bytes.length > 0) {
            return bytes;
        } else {
            try {
                is = context.getResources().getAssets().open(modelName);
                bytes = new byte[((InputStream)is).available()];
                ((InputStream)is).read(bytes);
            } catch (IOException var34) {
                var34.printStackTrace();
            } finally {
                try {
                    if (is != null) {
                        ((InputStream)is).close();
                    }
                } catch (IOException var32) {
                }

            }

            return bytes;
        }
    }

    public static Bitmap getBitmap(Context context, String imageName) {
        InputStream is = null;

        try {
            AssetManager assetManager = context.getResources().getAssets();
            is = assetManager.open(imageName);
            Bitmap var4 = BitmapFactory.decodeStream(is);
            return var4;
        } catch (Exception var14) {
            var14.printStackTrace();
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException var13) {
                var13.printStackTrace();
            }

        }

        return null;
    }

    public static byte[] getByteArrayFromAssets(Context context, String imageName) {
        byte[] bytes = null;

        try {
            InputStream is = context.getAssets().open(imageName);
            bytes = new byte[is.available()];
            is.read(bytes);
            is.close();
        } catch (IOException var4) {
            Log.e("zq", "e-->" + var4.getMessage());
            var4.printStackTrace();
        }

        return bytes;
    }

    public static void saveFeatureType(int userId, String featureType, String savePath) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("\n");
        stringBuffer.append(" userID: ");
        stringBuffer.append(userId);
        stringBuffer.append("  ");
        stringBuffer.append(" featureType: ");
        stringBuffer.append(featureType);
        stringBuffer.append("  ");
        saveStringToFile("featureType", savePath + "_" + dateFormat + ".txt", stringBuffer.toString());
    }

    public static void saveRecognizeErrorNum(int errorNum, String savePath) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(" 失败次数: ");
        stringBuffer.append(errorNum);
        saveStringToFile("featureType", savePath + "_" + dateFormat + ".txt", stringBuffer.toString());
    }

    private static boolean saveStringToFile(String filePath, String savePath, String landmark) {
        boolean result = false;
        FileOutputStream out = null;
        File dir = new File(Environment.getExternalStorageDirectory() + File.separator + filePath);
        if (!dir.exists()) {
            dir.mkdir();
        }

        File f = new File(dir, savePath);

        try {
            out = new FileOutputStream(f, true);
            out.write(landmark.getBytes());
            result = true;
        } catch (Exception var20) {
            var20.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.flush();
                } catch (IOException var19) {
                    var19.printStackTrace();
                }
            }

            if (out != null) {
                try {
                    out.close();
                } catch (IOException var18) {
                    var18.printStackTrace();
                }
            }

        }

        return result;
    }

    static {
        dateFormat = sdf.format(new Date());
    }
}
